using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WP.Commons
{
    public partial class frmMsgBox : Form
    {
        DialogResult mReturn;
        MessageBoxButtons m_btnButtons=0;
        MessageBoxDefaultButton m_btnDefaultButton=0;
        //MessageBoxIcon m_tpMsgIcon;
        const int TopMargin    = 100;
        const int BorderMargin = 100;
        
        public frmMsgBox()
        {
            InitializeComponent();

        }
        public DialogResult Msg(string Prompt, MessageBoxButtons Buttons, MessageBoxDefaultButton DefaultButton, MessageBoxIcon MsgIcon, string Title, string HelpFile, string Context)
        {
            /*
                        int i;
            //            Control btnObj;
                        string[] strPrompt;
                        string strMaxLine;
                        int iBtnWidth;
                        bool blnNeedIcon;

            //            SysVar.KeepMsgTimes = 3;

                        lblmsg.Text = Prompt;
                        if (Title.Trim() == "")
                            this.Text = "";//App.Title;
                        else
                            this.Text = Title;
            
                        m_btnButtons        = Buttons;
                        m_btnDefaultButton  = DefaultButton;
                        m_tpMsgIcon         = MsgIcon;

                        //calculate lable width
                        strMaxLine = "";
                        strPrompt = Prompt.Split('\r');//Split(Prompt, Chr(13))
                        for (i = 0;i < strPrompt.Length; i++)
                        {
                            if (strPrompt[i].Trim().Length > strMaxLine.Length)
                            {
                               strMaxLine = strPrompt[i].Trim();
                            }
                        }

                        blnNeedIcon = false;
                        foreach (Control btnObj in this.Controls)
                        {
                           if (btnObj.GetType().Name == "Button")
                           {
                              btnObj.Visible = false;
                           }
                        }
            
                        //Deal with Icon
                        //for (i = 0; i < this.iglMsgBoxType.Images.Count - 1;i++)
                        //{
                        //    this.imgIcon(i).Visible = false;
                        //    this.imgIcon(i).Top = TopMargin;
                        //    this.imgIcon(i).Left = BorderMargin;
                        //}
                            this.picMsgBox.Top  = TopMargin;
                            this.picMsgBox.Left = BorderMargin;
                            //MsgBoxEx.Show(dd,sdf,sss,df, ,,,,,,,,,,,

                        if (MsgIcon == MessageBoxIcon.Information)
                        {
            //                imgIcon(IconNo.iInformation).Visible = true;
                            this.picMsgBox.Image = this.iglMsgBoxType.Images[0];
                            blnNeedIcon = true;
                        }
                        else if (MsgIcon == MessageBoxIcon.Exclamation)
                        {
            //                imgIcon(IconNo.iExclamation).Visible = true;
                            this.picMsgBox.Image = this.iglMsgBoxType.Images[1];
                            blnNeedIcon = true;
                        }
                        else if (MsgIcon == MessageBoxIcon.Question)
                        {
            //               imgIcon(IconNo.iQuestion).Visible = true;
                           this.picMsgBox.Image = this.iglMsgBoxType.Images[2];
                           blnNeedIcon = true;
                        }
                        else if (MsgIcon == MessageBoxIcon.Error)
                        {
            //                imgIcon(IconNo.iCritical).Visible = true;
                            this.picMsgBox.Image = this.iglMsgBoxType.Images[3];
                            blnNeedIcon = true;
                        }
            
            
                        //deal with Postion

                        if (blnNeedIcon)
                            this.lblmsg.Left = picMsgBox.Left + picMsgBox.Left + picMsgBox.Width + BorderMargin;
                        else
                            this.lblmsg.Left = picMsgBox.Left + BorderMargin;
            
            
                        //deal with Button
            
                        bool blnNeedHelp;
                        int iBtnBetweenWd;
                        iBtnBetweenWd = 30;

                        ////if (Buttons == vbMsgBoxHelpButton)
                        ////{
                        ////   iBtnWidth = btnHelp.Width + iBtnBetweenWd;
                        ////   btnHelp.Visible = true;
                        ////   blnNeedHelp = true;
                        ////}
                        ////else
                        ////{
                           iBtnWidth = 0;
                           blnNeedHelp = false;
                        ////}
            
             */
            // if ((Buttons && vbRetryCancel) == vbRetryCancel)

            lblmsg.Text = Prompt;
            if (Title.Trim() == "")
                this.Text = "";//App.Title;
            else
                this.Text = Title;

            if (Buttons == MessageBoxButtons.RetryCancel)
            {
                this.btnRetry.Visible = true;
                this.btnCancel.Visible = true;
                //iBtnWidth = iBtnWidth + btnRetry.Width + iBtnBetweenWd + btnCancel.Width + iBtnBetweenWd;
            }
            else if (Buttons == MessageBoxButtons.YesNo)
            {
                this.btnYes.Visible = true;
                this.btnNo.Visible = true;
                //iBtnWidth = iBtnWidth + btnYes.Width + iBtnBetweenWd + btnNo.Width + iBtnBetweenWd;
            }
            else if (Buttons == MessageBoxButtons.YesNoCancel)
            {
                this.btnYes.Visible = true;
                this.btnNo.Visible = true;
                this.btnCancel.Visible = true;
                //iBtnWidth = iBtnWidth + btnYes.Width + iBtnBetweenWd + btnNo.Width + iBtnBetweenWd + btnCancel.Width + iBtnBetweenWd;
            }
            else if (Buttons == MessageBoxButtons.AbortRetryIgnore)
            {
                this.btnAbort.Visible = true;
                this.btnRetry.Visible = true;
                this.btnIgnore.Visible = true;
                //iBtnWidth = iBtnWidth + btnAbort.Width + iBtnBetweenWd + btnRetry.Width + iBtnBetweenWd + btnIgnore.Width + iBtnBetweenWd;
            }
            else if (Buttons == MessageBoxButtons.OKCancel)
            {
                this.btnOK.Visible = true;
                this.btnCancel.Visible = true;
                //iBtnWidth = iBtnWidth + btnOK.Width + iBtnBetweenWd + btnCancel.Width + iBtnBetweenWd;
            }
            else if (Buttons == MessageBoxButtons.OK)
            {
                this.btnOK.Visible = true;
                //iBtnWidth = iBtnWidth + btnOK.Width + iBtnBetweenWd;
            }

            /*
                lblMax.Text = strMaxLine;
                lblMax.Width = lblMax.Width + 1 * Convert.ToInt32(this.lblMax.Font.Size) * 11;
                if (lblMax.Width > 11130)
                    this.Width = 11130 + picMsgBox.Width + BorderMargin * (blnNeedIcon==true?3:2);
                else if (lblMax.Width < iBtnWidth)
                    this.Width = iBtnWidth + picMsgBox.Width + BorderMargin * (blnNeedIcon==true?3:2);
                else
                    this.Width = lblMax.Width + picMsgBox.Width + BorderMargin * (blnNeedIcon==true?3:2);
  
    
                lblmsg.Top = TopMargin;
                lblMax2.Top = TopMargin;
                lblMax.Top = TopMargin;
                lblMax2.Left = lblmsg.Left;
                lblMax.Left = lblmsg.Left;
                if (blnNeedIcon)
                   this.lblmsg.Width = this.Width - picMsgBox.Width - BorderMargin * 3;
                else
                   this.lblmsg.Width = this.Width - BorderMargin * 2;
    
                this.lblMax2.Width = lblmsg.Width;
                lblMax2.Text = lblmsg.Text;
    
                if (lblMax2.Height > 7000)
                   lblMax2.Height = 7000;
    
    
                this.lblmsg.Height = lblMax2.Height;
    
    
                this.Height = this.btnCancel.Height + (lblMax2.Height < this.picMsgBox.Height ? this.picMsgBox.Height: lblMax2.Height) + TopMargin * 2;
                this.Height = this.Height + (this.Height - this.Height);
            */
            if (Buttons == MessageBoxButtons.RetryCancel)
            {
                //this.btnRetry.Left = (this.Width - iBtnWidth) / 2;
                //btnCancel.Left = btnRetry.Left + iBtnBetweenWd + btnRetry.Width;
                //if (blnNeedHelp)
                //    btnHelp.Left = btnCancel.Left + iBtnBetweenWd + btnCancel.Width;
                mReturn = DialogResult.Cancel;
            }
            else if (Buttons == MessageBoxButtons.YesNo)
            {
                //this.btnYes.Left = (this.Width - iBtnWidth) / 2;
                //btnNo.Left = btnYes.Left + iBtnBetweenWd + btnYes.Width;
                //if (blnNeedHelp)
                //    btnHelp.Left = btnNo.Left + iBtnBetweenWd + btnNo.Width;
                mReturn = DialogResult.No;
            }
            else if (Buttons == MessageBoxButtons.YesNoCancel)
            {
                //this.btnYes.Left = (this.Width - iBtnWidth) / 2;
                //btnNo.Left = btnYes.Left + iBtnBetweenWd + btnYes.Width;
                //btnCancel.Left = btnNo.Left + iBtnBetweenWd + btnYes.Width;
                //if (blnNeedHelp)
                //    btnHelp.Left = btnCancel.Left + iBtnBetweenWd + btnCancel.Width;
                mReturn = DialogResult.Cancel;
            }
            else if (Buttons == MessageBoxButtons.AbortRetryIgnore)
            {
                //this.btnAbort.Left = (this.Width - iBtnWidth) / 2;
                //btnRetry.Left = btnAbort.Left + iBtnBetweenWd + btnAbort.Width;
                //btnIgnore.Left = btnRetry.Left + iBtnBetweenWd + btnRetry.Width;
                //if (blnNeedHelp)
                //    btnHelp.Left = btnIgnore.Left + iBtnBetweenWd + btnIgnore.Width;
                mReturn = DialogResult.Ignore;
            }
            else if (Buttons == MessageBoxButtons.OKCancel)
            {
                //this.btnOK.Left = (this.Width - iBtnWidth) / 2;
                //btnCancel.Left = btnOK.Left + iBtnBetweenWd + btnOK.Width;
                //if (blnNeedHelp)
                //    btnHelp.Left = btnCancel.Left + iBtnBetweenWd + btnCancel.Width;
                mReturn = DialogResult.Cancel;
            }
            else if (Buttons == MessageBoxButtons.OK)
            {
                //this.btnOK.Left = (this.Width - iBtnWidth) / 2;
                //if (blnNeedHelp)
                //    btnHelp.Left = btnOK.Left + iBtnBetweenWd + btnOK.Width;
                mReturn = DialogResult.OK;
            }


            //set defaut button
            if (Buttons == MessageBoxButtons.RetryCancel)
            {
                if (m_btnDefaultButton == MessageBoxDefaultButton.Button3)
                { }
                else if (m_btnDefaultButton == MessageBoxDefaultButton.Button2)
                {
                    //if (blnNeedHelp)
                    //    this.AcceptButton = btnHelp;
                }
                else if (m_btnDefaultButton == MessageBoxDefaultButton.Button1)
                    this.AcceptButton = btnCancel;
                else
                    this.AcceptButton = btnRetry;
            }
            else if (Buttons == MessageBoxButtons.YesNo)
            {
                if (m_btnDefaultButton == MessageBoxDefaultButton.Button3)
                { }
                else if (m_btnDefaultButton == MessageBoxDefaultButton.Button2)
                {
                    //if (blnNeedHelp)
                    //    this.AcceptButton = btnHelp;
                }
                else if (m_btnDefaultButton == MessageBoxDefaultButton.Button1)
                    this.AcceptButton = btnNo;
                else
                    this.AcceptButton = btnYes;
            }
            else if (Buttons == MessageBoxButtons.YesNoCancel)
            {
                if (m_btnDefaultButton == MessageBoxDefaultButton.Button3)
                {
                    //if (blnNeedHelp)
                    //    this.AcceptButton = btnHelp;
                }
                else if (m_btnDefaultButton == MessageBoxDefaultButton.Button2)
                    this.AcceptButton = btnCancel;
                else if (m_btnDefaultButton == MessageBoxDefaultButton.Button1)
                    this.AcceptButton = btnNo;
                else
                    this.AcceptButton = btnYes;
            }
            else if (Buttons == MessageBoxButtons.AbortRetryIgnore)
            {
                if (m_btnDefaultButton == MessageBoxDefaultButton.Button3)
                {
                    //if (blnNeedHelp)
                    //    this.AcceptButton = btnHelp;
                }
                else if (m_btnDefaultButton == MessageBoxDefaultButton.Button2)
                    this.AcceptButton = btnIgnore;
                else if (m_btnDefaultButton == MessageBoxDefaultButton.Button1)
                    this.AcceptButton = btnRetry;
                else
                    this.AcceptButton = btnAbort;
            }
            else if (Buttons == MessageBoxButtons.OKCancel)
            {
                if (m_btnDefaultButton == MessageBoxDefaultButton.Button3)
                { }
                else if (m_btnDefaultButton == MessageBoxDefaultButton.Button2)
                {
                    //if (blnNeedHelp)
                    //    this.AcceptButton = btnHelp;
                }
                else if (m_btnDefaultButton == MessageBoxDefaultButton.Button1)
                    this.AcceptButton = btnCancel;
                else
                    this.AcceptButton = btnOK;
            }
            else if (Buttons == MessageBoxButtons.OK)
            {
                if (m_btnDefaultButton == MessageBoxDefaultButton.Button3)
                { }
                else if (m_btnDefaultButton == MessageBoxDefaultButton.Button2)
                { }
                else if (m_btnDefaultButton == MessageBoxDefaultButton.Button1)
                {
                    //if (blnNeedHelp)
                    //    this.AcceptButton = btnHelp;
                }
                else
                    this.AcceptButton = btnOK;
            }

            /*
                foreach (Control btnObj in this.Controls)
                {
                   if (btnObj.GetType().Name == "Button")
                      btnObj.Top = this.Height - this.btnAbort.Height;
       
                }
            */
            if (!BaseFunctions.IsNumeric(SysVar.KeepMsgTimes.ToString()))
                TmMsgBox.Enabled = false;
            else if (SysVar.KeepMsgTimes <= 0)
                TmMsgBox.Enabled = false;
            else
            {
                TmMsgBox.Interval = Convert.ToInt32(SysVar.KeepMsgTimes * 1000);
                TmMsgBox.Enabled = true;
            }

            this.TopMost = true;
            //Application.DoEvents();
            return mReturn;
        }


        #region Private Events
        private void frmMsgBox_Load(object sender, EventArgs e)
        {

        }

        private void TmMsgBox_Tick(object sender, EventArgs e)
        {
            //set defaut bnutton
            if (m_btnButtons == MessageBoxButtons.RetryCancel)
            {
                if (m_btnDefaultButton == MessageBoxDefaultButton.Button3)
                   PressCancel();
               else if (m_btnDefaultButton == MessageBoxDefaultButton.Button2)
                   PressRetry();
               else if (m_btnDefaultButton == MessageBoxDefaultButton.Button1)
                   PressCancel();
               else
                   PressRetry();
               
            }
            else if (m_btnButtons == MessageBoxButtons.YesNo)
            {               
               if (m_btnDefaultButton == MessageBoxDefaultButton.Button3)
                   PressNo();
               else if (m_btnDefaultButton == MessageBoxDefaultButton.Button2)
                   PressNo();
               else if (m_btnDefaultButton == MessageBoxDefaultButton.Button1)
                   PressNo();
               else
                   PressYes();
               
            }  
            else if (m_btnButtons == MessageBoxButtons.YesNoCancel)
            {
               if (m_btnDefaultButton == MessageBoxDefaultButton.Button3)
                   PressCancel();
               else if (m_btnDefaultButton == MessageBoxDefaultButton.Button2)
                   PressCancel();
               else if (m_btnDefaultButton == MessageBoxDefaultButton.Button1)
                   PressNo();
               else
                   PressYes();
               
            }
            else if (m_btnButtons == MessageBoxButtons.AbortRetryIgnore)
            {
               if (m_btnDefaultButton == MessageBoxDefaultButton.Button3)
                   PressAbort();
               else if (m_btnDefaultButton == MessageBoxDefaultButton.Button2)
                   PressIgnore();
               else if (m_btnDefaultButton == MessageBoxDefaultButton.Button1)
                   PressRetry();
               else
                   PressAbort();
               
            }
            else if (m_btnButtons == MessageBoxButtons.OKCancel)
            {
               if (m_btnDefaultButton == MessageBoxDefaultButton.Button3)
                   PressCancel();
               else if (m_btnDefaultButton == MessageBoxDefaultButton.Button2)
                   PressCancel();
               else if (m_btnDefaultButton == MessageBoxDefaultButton.Button1)
                   PressCancel();
               else
                   PressOK();
               
            }
            else if (m_btnButtons == MessageBoxButtons.OK)
            {
                PressOK();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            PressOK();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            PressCancel();
        }

        private void btnAbort_Click(object sender, EventArgs e)
        {
            PressAbort();
        }

        private void btnRetry_Click(object sender, EventArgs e)
        {
            PressRetry();
        }

        private void btnIgnore_Click(object sender, EventArgs e)
        {
            PressIgnore();
        }

        private void btnYes_Click(object sender, EventArgs e)
        {
            PressYes();
        }

        private void btnNo_Click(object sender, EventArgs e)
        {
            PressNo();
        }

        #endregion

        #region Private Functions

        private void PressOK()
        {
            mReturn = DialogResult.OK;
            this.Close();
        }

        private void PressCancel()
        {
            mReturn = DialogResult.Cancel;
            this.Close();
        }

        private void PressAbort()
        {
            mReturn = DialogResult.Abort;
            this.Close();
        }

        private void PressRetry()
        {
            mReturn = DialogResult.Retry;
            this.Close();
        }

        private void PressIgnore()
        {
            mReturn = DialogResult.Ignore;
            this.Close();
        }

        private void PressYes()
        {
            mReturn = DialogResult.Yes;
            this.Close();
        }

        private void PressNo()
        {
            mReturn = DialogResult.No;
            this.Close();
            //MessageBox.Show();
        }

        #endregion




    }
}